package com.odm.communitymanage.ui.activity

import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.UriUtils
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.NetCallBack
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.util.GlideUtils
import com.odm.communitymanage.util.Utils
import com.rxjava.rxlife.lifeOnMain
import com.zhihu.matisse.Matisse
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_post_activity.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 发布活动页面
 * @author: ODM
 * @date: 2020/4/20
 */
class PostActivityFragment : BaseFragment() {

    var disposable : Disposable?= null

    var photoUris: MutableList<Uri>  ?= mutableListOf()



    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }
    
    override fun initViews() {

        btnPostActivityPhoto?.setOnClickListener {
            //跳转页面选择图片，当选择了图片后，依次让iv1,iv2,iv3 设为Visible，iv3也可见后将添加图片按钮设为inVisible！
            Utils.openMatissePage(this)
        }

        btnPostActivityInfo?.setOnClickListener {
            val topic = etTopic_PostActivity.text.toString()
            val matters = etMatters_PostActivity.text.toString()
            val  desc = etDescription_PostActivity.text.toString()
            //调用上传 闲置信息
            postActivityInfo(topic,desc,matters , photoUris)
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //加载从 图库中选择的图片，每次为多张或单张，最多放入3张
        if (requestCode == Constants.REQUEST_IMAGE_CODE && data != null) {
            val result = Matisse.obtainResult(data)
            val uriList  = result?.toMutableList()
            uriList?.let {
                for (uri in uriList as MutableList<Uri>) {
                    var target : ImageView?= null
                    if (ivPostActivityPhoto_1.visibility != View.VISIBLE) {
                        target = ivPostActivityPhoto_1
                    } else if (ivPostActivityPhoto_2.visibility != View.VISIBLE) {
                        target = ivPostActivityPhoto_2
                    } else if (ivPostActivityPhoto_3.visibility != View.VISIBLE) {
                        target = ivPostActivityPhoto_3
                        btnPostActivityPhoto.visibility = View.INVISIBLE
                    }
                    target?.let {
                        GlideUtils.getInstance()
                            .loadImageUri(requireContext(), uri, target)
                        target.visibility = View.VISIBLE
                        photoUris?.add(uri)
                    }
                }

            }
        }
    }


    private  fun postActivityInfo(topic:String , description:String ,
                              matters:String , photos:MutableList<Uri>?){
        if(topic == "" || description == ""){
            ToastUtils.showShort("请填写 活动主题 与 活动描述")
            return
        }
        if(photos?.isEmpty() == true){
            ToastUtils.showShort("请选择合适的图片")
            return
        }
        val param = RxHttp.postForm(ApiService.POST_ACTIVITY)
            .add("topic",topic)
            .add("description",description)
            .add("matters",matters)

        //添加文件
        photos?.let {
            for(uri in photos){
                val photo =  Utils.getPhotoFileByUri(uri , true)
                param.addFile("file",photo)
            }
        }

        disposable = param.upload {
            LogUtils.d("上传进度:  ${it.progress}")
        }.asClass(NetCallBack::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code != 1){
                    ToastUtils.showShort(it.message)
                }else {
                    //上传成功
                    showSuccessSnack(getString(R.string.post_success))
                    //返回上一页
                    pop()
                }
            },{
                //出现异常
                LogUtils.e("上传异常 "+ it.localizedMessage)
                ToastUtils.showShort(it.message)
                it.printStackTrace()
            })


    }
    

    override fun getLayoutId(): Int {
        return R.layout.fragment_post_activity
    }
}